package com.example.dream.talentbase.controller;

import com.example.dream.talentbase.entiy.AllGradesTableEntiy;
import com.example.dream.talentbase.entiy.RecieveEntityTwo;
import com.example.dream.talentbase.service.TalentBaseService;
import com.example.dream.talentbase.service.impl.TalentBaseserviceImpl;
import com.example.dream.talentbase.util.anatomiamath.ATM;
import com.example.dream.talentbase.util.anatomiamath.imp.ATMImp;
import com.example.dream.talentbase.util.consciousnessmasth.CS;
import com.example.dream.talentbase.util.consciousnessmasth.imp.CSImp;
import com.example.dream.talentbase.util.hereditymasth.HD;
import com.example.dream.talentbase.util.hereditymasth.imp.HDImp;
import com.example.dream.talentbase.util.physiologymasth.PG;
import com.example.dream.talentbase.util.physiologymasth.imp.PGImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * @className TalentBaseController
 * @Description TODO
 * @Autor WUAO
 * @Date 2022/8/16 20 44
 */
@Controller
@RequestMapping("/children")
public class ChildrenController {
    ATM atm=new ATMImp();
    @Autowired
    TalentBaseService service=new TalentBaseserviceImpl();
    @RequestMapping("/insert")
    @ResponseBody
    public Boolean insert(AllGradesTableEntiy entiy, HttpServletRequest request, HttpServletResponse response){
       AllGradesTableEntiy entiy1=entiy;
        return service.insert(entiy1);

    }
    //保留两位小数，返回值为String
    public static String format1(double value) {

        BigDecimal bd = new BigDecimal(value);
        bd = bd.setScale(2, RoundingMode.HALF_UP);
        return bd.toString();
    }
    //保留两位小数，返回值为Double
    public static Double format2(double value) {

        BigDecimal bd = new BigDecimal(value);
        bd = bd.setScale(2, RoundingMode.HALF_UP);
        Double d=Double.parseDouble(bd.toString());
        return d;
    }

    @RequestMapping("/receive")
    public String receive(HttpServletResponse response, HttpServletRequest request, RecieveEntityTwo entity){
        Map<String,Double> m=new HashMap<>();
        //解刨学统计方法接口
        ATM atm=new ATMImp();
        //意识形态学统计方法接口
        CS cs=new CSImp();
        //遗传基因学统计方法接口
        HD hd=new HDImp();
        //运动生理学统计方法接口
        PG pg=new PGImp();
        if(entity.getGenetics_profession_1()!=null){
            m=hd.brain(m);
        }else if(entity.getGenetics_figure_2()!=null){
            m=hd.vit(m);
        }else {
            m=hd.sport(m);
        }

        if(entity.getGenetics_physical_quality_1()!=null){
            m=hd.quaA(m);
        }else if(entity.getGenetics_physical_quality_2()!=null){
            m=hd.quaB(m);
        }else{
            m=hd.quaC(m);
        }

        if(entity.getGenetics_figure_1()!=null){
            m=hd.heiA(m);
        }else if(entity.getGenetics_figure_2()!=null){
            m=hd.heiB(m);
        }else{
            m=hd.heiC(m);
        }

        if(entity.getGenetics_height_1()!=null){
            m=hd.shaA(m);
        }else if(entity.getGenetics_height_2()!=null){
            m=hd.shaB(m);
        }else{
            m=hd.shaC(m);
        }

        if(entity.getAnatomy_stature_1()!=null){
            m=atm.htA(m);
        }else if(entity.getAnatomy_stature_2()!=null){
            m=atm.htB(m);
        }else{
            m=atm.htC(m);
        }

        if(entity.getAnatomy_weight_1()!=null){
            m=atm.wtA(m);
        }else if(entity.getAnatomy_weight_2()!=null){
            m=atm.wtB(m);
        }else{
            m=atm.wtC(m);
        }

        if(entity.getAnatomy_wingspan_1()!=null){
            m=atm.armA(m);
        } else if (entity.getAnatomy_wingspan_2()!=null) {
            m=atm.armB(m);
        }else {
            m=atm.armB(m);
        }

        if(entity.getAnatomy_leg_length_1()!=null){
            m=atm.legA(m);
        }else if(entity.getAnatomy_leg_length_2()!=null){
            m=atm.legB(m);
        }else{
            m=atm.legC(m);
        }

        if(entity.getQuality_endurance_1()!=null){
            m=pg.nikoA(m);
        }else if(entity.getQuality_endurance_2()!=null){
            m=pg.nikoB(m);
        }else{
            m=pg.nikoC(m);
        }

        if(entity.getQuality_explosive_1()!=null){
            m=pg.breakOutA(m);
        }else if(entity.getQuality_explosive_2()!=null){
            m=pg.breakOutB(m);
        }else{
            m=pg.breakOutC(m);
        }

        if(entity.getQuality_sensitive_1()!=null){
            m=pg.sensitiveA(m);
        } else if (entity.getQuality_sensitive_2()!=null) {
            m=pg.sensitiveB(m);
        }else {
            m=pg.sensitiveC(m);
        }

        if(entity.getQuality_speed_1()!=null){
            m=pg.speedA(m);
        } else if (entity.getQuality_speed_2()!=null) {
            m=pg.speedB(m);
        }else {
            m=pg.speedC(m);
        }

        if(entity.getQuality_flexible_1()!=null){
            m=pg.flexibilityA(m);
        }else if(entity.getQuality_flexible_2()!=null){
            m=pg.flexibilityB(m);
        }else {
            m=pg.flexibilityC(m);
        }

        if(entity.getQuality_reaction_1()!=null){
            m=pg.reactionA(m);
        }else if(entity.getQuality_reaction_2()!=null){
            m=pg.reactionB(m);
        }else {
            m=pg.reactionC(m);
        }

        if(entity.getQuality_jump_1()!=null){
            m=pg.springA(m);
        }else if(entity.getQuality_jump_2()!=null){
            m=pg.springB(m);
        }else{
            m=pg.springC(m);
        }

        if(entity.getQuality_upper_body_strength_1()!=null){
            m=pg.upperLimbA(m);
        } else if (entity.getQuality_upper_body_strength_2()!=null) {
            m=pg.upperLimbB(m);
        }else{
            m=pg.upperLimbC(m);
        }

        if(entity.getQuality_lower_limb_strength_1()!=null){
            m=pg.lowerLimbA(m);
        } else if (entity.getQuality_lower_limb_strength_2()!=null) {
            m=pg.lowerLimbB(m);
        }else{
            m=pg.lowerLimbC(m);
        }

        if(entity.getQuality_balance_1()!=null){
            m=pg.balanceA(m);
        }else if(entity.getQuality_balance_2()!=null){
            m=pg.balanceB(m);
        }else{
            m=pg.balanceC(m);
        }

        if(entity.getMentality_self_control_1()!=null){
            m=cs.selfControllA(m);
        } else if (entity.getMentality_self_control_2()!=null) {
            m=cs.selfControllB(m);
        }else{
            m=cs.selfControllC(m);
        }

        if(entity.getMentality_quality_1()!=null){
           m=cs.willD(m);
        } else if (entity.getMentality_quality_2()!=null) {
            m=cs.willC(m);
        }else if(entity.getMentality_quality_3()!=null){
            m=cs.willA(m);
        }else{
            m=cs.willB(m);
        }

        if(entity.getMentality_types_1()!=null){
            m=cs.mentallyA(m);
        }else if(entity.getMentality_types_2()!=null){
            m=cs.mentallyB(m);
        }else if(entity.getMentality_types_3()!=null){
            m=cs.mentallyC(m);
        }else{
            m=cs.mentallyD(m);
        }

        if(entity.getMentality_tactics_1()!=null){
            m=cs.warA(m);
        } else if (entity.getMentality_tactics_2()!=null) {
            m=cs.warB(m);
        }else{
            m=cs.warC(m);
        }

        if(entity.getMentality_teamwork_1()!=null){
            m=cs.teamA(m);
        } else if (entity.getMentality_teamwork_2()!=null) {
            m=cs.teamB(m);
        }else{
            m=cs.teamC(m);
        }

        if(entity.getMentality_mental_1()!=null){
            m=cs.hartA(m);
        }else if(entity.getMentality_mental_2()!=null){
            m=cs.hartB(m);
        }else{
            m=cs.hartC(m);
        }


         //取前四体育项
         Set<String> keySet = m.keySet();
         Double[] list= new Double[100];
         Integer cnt=0;
         String[] temp={"田赛跳跃类","田赛投掷类","举重","径赛短跑","游泳","径赛中长跑","滑雪","赛艇","帆船","体操","武术","体育舞蹈","足球","篮球","冰壶","羽毛球","排球","网球","摔跤","散打","马术","高尔夫球"};
         for (int i = 0; i < temp.length; i++) {
             if(m.get(temp[i])!=null){
                 String s1=format1(m.get(temp[i]));
                 list[cnt]=Double.parseDouble(s1);
                 cnt++;
             }
         }

        //打印查看数组情况
        for (int i = 0; i < cnt; i++) {
            System.out.print(list[i]+" ");
        }
        System.out.println();
        //冒泡排序算法
        Integer len=cnt;
        for (int i = 0; i < len; i++) { //控制循环次数，比长度少一次。
            for (int j = 0; j < len-i-1; j++) { //后面排好的值不需要进行比较，所以减去i。
                if(list[j]<list[j+1]) {
                    Double tem = list[j];
                    list[j] = list[j+1];
                    list[j+1] = tem;
                }
            }
        }

        //打印查看数组情况
        for (int i = 0; i < cnt; i++) {
            System.out.print(list[i]+" ");
        }
        System.out.println();
        //去重列表
        ArrayList<String> list_tem=new ArrayList<>();
        //去重标志
        Boolean flag=false;
        Integer a=0;
        for (int i = 0; i < 4; i++) {
            System.out.println(i);
            for(String key:keySet){

                if(format2(m.get(key)).equals(list[i])){

                    for(String s:list_tem){
                        if(s==key){
                            System.out.println("flag");
                            flag=true;
                        }
                    }
                    if(flag==false){
                        String index="status"+a;
                        a++;
                        request.getSession().setAttribute(index,key+" "+format2(m.get(key)));
                        list_tem.add(key);
                        System.out.println(key+" "+list[i]+" "+index);
                    }
                    flag=false;

                }
            }

        }
        request.getSession().setAttribute("user_name",entity.getUser_name());
        request.getSession().setAttribute("sex",entity.getAnatomy_sex());
        request.getSession().setAttribute("age",entity.getAnatomy_years());
        request.getSession().setAttribute("phone",entity.getPhone_number());

        System.out.println();
        System.out.println("田赛跳跃类"+m.get("田赛跳跃类")+" "+
                "田赛投掷类"+ m.get("田赛投掷类")+" "+
                "举重"+ m.get("举重")+" "+
                "径赛短跑"+ m.get("径赛短跑")+" "+
                "游泳"+  m.get("游泳")+" "+
                "滑雪"+  m.get("滑雪")+" "+
                "赛艇"+  m.get("赛艇")+" "+
                "帆船"+  m.get("帆船")+" "+
                "体操"+ m.get("体操")+" "+
                "武术"+  m.get("武术")+" "+
                "体育舞蹈"+ m.get("体育舞蹈")+" "+
                "滑板"+ m.get("滑板")+" "+
                "足球"+ m.get("足球")+" "+
                "篮球"+ m.get("篮球")+" "+
                "冰壶"+ m.get("冰壶")+" "+
                "乒乓球"+ m.get("乒乓球")+" "+
                "羽毛球"+ m.get("羽毛球")+" "+
                "排球"+ m.get("排球")+" "+
                "网球"+ m.get("网球")+" "+
                "摔跤"+ m.get("摔跤")+" "+
                "散打"+ m.get("散打")+" "+
                "马术"+ m.get("马术")+" "+
                "高尔夫球"+ m.get("高尔夫球")+" "
        );

        return "index/result";
    }

}
